package com.zly.framework.rbac.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zly.framework.rbac.model.entity.Permission;
import com.zly.framework.rbac.service.PermissionService;
import com.zly.framework.rbac.model.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/sys/permission")
@RestController
@Tag(name = "权限接口")
public class PermissionController {
    @Resource
    private PermissionService permissionServiceImpl;

    @Operation(summary = "分页获取权限信息")
    @GetMapping
    public Result<IPage<Permission>> getByPage(@RequestParam(name = "current", defaultValue = "1") Long current,
                                               @RequestParam(name = "size", defaultValue = "50") Long size) {
        IPage<Permission> permissionPage = new Page<>(current, size);
        IPage<Permission> page = permissionServiceImpl.page(permissionPage);
        return Result.SUCCESS(page);
    }

    @Operation(summary = "添加权限信息")
    @PostMapping
    public Result<Object> save(Permission permission) {
        permissionServiceImpl.save(permission);
        return Result.SUCCESS();
    }

    @Operation(summary = "修改权限信息")
    @PutMapping
    public Result<Object> update(Permission permission) {
        permissionServiceImpl.updateById(permission);
        return Result.SUCCESS();
    }

    @Operation(summary = "删除权限信息")
    @DeleteMapping
    public Result<Object> delete(@RequestParam List<Long> permissionIds) {
        // todo:检查权限与角色的关系
        permissionServiceImpl.removeBatchByIds(permissionIds);
        return Result.SUCCESS();
    }
}
